package com.hotau.account;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import com.hotau.HotauActivity;
import com.hotau.HotauConstants;
import com.hotau.R;
import com.hotau.account.mode.balance.BalanceEntity;
import com.hotau.account.mode.balance.DetailAdapter;
import com.hotau.member.service.MemberService;
import com.hotau.util.DialogUtil;
import com.hotau.util.HttpRequestAsynTask;
import com.hotau.util.HttpUtil;

/**
 * 收支汇总
 * @author liujia (liujia9234@126.com)
 * @data @2011-12-11
 * @version 1.0
 * <ul>
 * 		<li>1.0 创建</li>
 * </ul>
 */
@SuppressLint("ResourceAsColor")
public class BlanceFragment extends Fragment implements OnClickListener {

	private Button ballace_btn_all = null;
	private Button ballace_btn_income = null;
	private Button ballace_btn_pay = null;
	private Button ballace_more = null;
	private ListView ballace_lv = null;
	public DetailAdapter adapter = null;
	
	private int pageIndex = 1; 
	/**
	 * 异步请求Task
	 */
	HttpRequestAsynTask ballace_task = null; //获取汇总数据
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initAllWidget();
		initAllEvent();
		ballace_btn_all.setTextColor(R.color.red);
		getData(3);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.account_balance, container,false);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.account_ballace_btn_all:
			ballace_btn_all.setBackgroundResource(R.drawable.account_balance_all_btn_bg_select);
			ballace_btn_all.setOnClickListener(null);
			ballace_btn_income.setBackgroundResource(R.drawable.account_balance_income_btn_bg_default);
			ballace_btn_income.setOnClickListener(this);
			ballace_btn_pay.setBackgroundResource(R.drawable.account_balance_pay_btn_bg_default);
			ballace_btn_pay.setOnClickListener(this);
			getData(3);
			break;
		case R.id.account_ballace_btn_income:
			ballace_btn_all.setBackgroundResource(R.drawable.account_balance_all_btn_bg_default);
			ballace_btn_all.setOnClickListener(this);
			ballace_btn_income.setBackgroundResource(R.drawable.account_balance_income_btn_bg_select);
			ballace_btn_income.setOnClickListener(null);
			ballace_btn_pay.setBackgroundResource(R.drawable.account_balance_pay_btn_bg_default);
			ballace_btn_pay.setOnClickListener(this);
			getData(1);
			break;
		case R.id.account_ballace_btn_pay:
			ballace_btn_all.setBackgroundResource(R.drawable.account_balance_all_btn_bg_default);
			ballace_btn_all.setOnClickListener(this);
			ballace_btn_income.setBackgroundResource(R.drawable.account_balance_income_btn_bg_default);
			ballace_btn_income.setOnClickListener(this);
			ballace_btn_pay.setBackgroundResource(R.drawable.account_balance_pay_btn_bg_select);
			ballace_btn_pay.setOnClickListener(null);
			getData(2);
			break;
		case R.id.account_ballace_more:
			ballace_more.setEnabled(false);
			getData(type);
			break;
		default:
			break;
		}
	}
	
	private int type = 0;
	public void getData(int type){
		if(this.type != type){
			adapter.clearData();
			this.type = type;
			pageIndex = 1;
		}
		StringBuilder data = new StringBuilder();
		data.append("Form:token="+MemberService.getToken(getActivity()));
		data.append(";terminalCode=" + HotauConstants.getTerminalCode(getActivity()));
		data.append(";type="+type);
		data.append(";pageNo="+pageIndex);
		ballace_task = new HttpRequestAsynTask(getActivity()){
			@Override
			protected void doPostExecute(String result) {
				HotauActivity csmbpActivity = (HotauActivity) getActivity();
				//DialogUtil.showDialog(getActivity(), "", result);
				try {
					JSONObject data = new JSONObject(result);
					if(data != null && data.getInt("result") == 1){
						//获取收支汇总成功
					    if(data.getString("balanceList").equals(""))
					    {
					    	ballace_more.setVisibility(View.GONE);
					        return;
					    }
					    ballace_more.setVisibility(View.VISIBLE);
						initBallaceListView(data.getJSONArray("balanceList"));
					}else{
						//修改失败
						DialogUtil.showDialog(getActivity(), "", data.getString("errorMsg"));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					DialogUtil.showDialog(getActivity(), "", "登陆返回数据出错 ");
				}finally{
					ballace_more.setEnabled(true);
				}
			}
		};
		ballace_task.execute(HotauConstants.ACCOUNT_QUERY_BLANCE,data.toString(),HttpUtil.UTF8_ENCODING);
	}
	
	/**
	 * 初始化所有控件
	 */
	public void initAllWidget(){
		ballace_btn_all = (Button) getActivity().findViewById(R.id.account_ballace_btn_all);
		ballace_btn_income = (Button) getActivity().findViewById(R.id.account_ballace_btn_income);
		ballace_btn_pay = (Button) getActivity().findViewById(R.id.account_ballace_btn_pay);
		ballace_more = (Button) getActivity().findViewById(R.id.account_ballace_more);
		ballace_lv = (ListView) getActivity().findViewById(R.id.account_ballace_lv);
		adapter = new DetailAdapter(getActivity());
		ballace_lv.setAdapter(adapter);
	}
	
	/**
	 * 初始化所有事件
	 */
	public void initAllEvent() {
//		ballace_btn_all.setOnClickListener(this);
		ballace_btn_income.setOnClickListener(this);
		ballace_btn_pay.setOnClickListener(this);
		ballace_more.setOnClickListener(this);
	}
	
	
	
	/**
	 * 初始化收支数据
	 */
	public void initBallaceListView(JSONArray array){
//		adapter.clearData();
		if(array != null && array.length() > 0){
			pageIndex++;
			for(int i =0 ;i<array.length();i++){
				try {
					JSONObject json = array.getJSONObject(i);
					BalanceEntity entity = new BalanceEntity();
					entity.setCreateDate(json.getString("createDate"));
					entity.setTradeType(json.getString("tradeType"));
					entity.setAmount(json.getString("amount"));
					if(json.has("type")){
						entity.setType(json.getString("type"));
					}else{
						continue;
					}
					
					adapter.addItem(entity);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			adapter.notifyDataSetChanged();
		}
	}
	
}
